package com.ruoyi.project.qyxy.qua.domain;

import com.ruoyi.project.qyxy.enterprise.domain.EnterpriseInfo;
import com.ruoyi.project.tool.utils.DoubleUtils;
import org.springframework.security.web.PortResolverImpl;

import java.util.List;

public class JlScoreTemp {

    /**
     * 企业信息
     */
    private EnterpriseInfo enterpriseInfo;

    /**
     * 企业资质信息列表
     */
    private List<JlQuaTemp> quaTempList;

    /**
     * 得分
     */
    private Double score = 24D;

    /**
     * 甲乙 100
     */
    private final double JY_100 = 15;

    /**
     * 甲乙 90
     */
    private final double JY_90 = 13.5;

    /**
     * 甲乙 80
     */
    private final double JY_80 = 12;

    /**
     * 甲乙 70
     */
    private final double JY_70 = 10.5;

    /**
     * 甲乙 60
     */
    private final double JY_60 = 9;

    /**
     * 丙 100
     */
    private final double BING_100 = 5;

    /**
     * 丙 90
     */
    private final double BING_90 = 4.5;

    /**
     * 丙 80
     */
    private final double BING_80 = 4;

    /**
     * 丙 70
     */
    private final double BING_70 = 3.5;

    /**
     * 丙 60
     */
    private final double BING_60 = 3;


    /**
     * 综合 100
     */
    private final double ZH_100 = 60;

    /**
     * 综合 90
     */
    private final double ZH_90 = 54;

    /**
     * 综合 80
     */
    private final double ZH_80 = 48;

    /**
     * 综合 70
     */
    private final double ZH_70 = 42;

    /**
     * 综合 60
     */
    private final double ZH_60 = 36;


    public JlScoreTemp(EnterpriseInfo enterpriseInfo, List<JlQuaTemp> quaTempList) {
        this.enterpriseInfo = enterpriseInfo;
        this.quaTempList = quaTempList;
    }

    /**
     * 计算分数
     */
    public void computeScore(){
        if(quaTempList.isEmpty()){
            score = 0d;
            System.out.println(enterpriseInfo.getOrgName()+"["+enterpriseInfo.getOrgCode()+"],得分:"+score);
            return;
        }
        if(!getAllFlag()){
            for (JlQuaTemp jlQuaTemp : quaTempList) {
                Double reduceScore = getReduceScore(jlQuaTemp);
                jlQuaTemp.setReduceScore(reduceScore.toString());
                Double sub = DoubleUtils.sub(score, reduceScore);
                score = sub < 0D ? 0 : sub;
            }
        }
        System.out.println(enterpriseInfo.getOrgName()+"["+enterpriseInfo.getOrgCode()+"],得分:"+score);
    }


    /**
     * 计算减分
     * @param jlQuaTemp
     * @return
     */
    public Double getReduceScore(JlQuaTemp jlQuaTemp){
        String quaRank = jlQuaTemp.getQuaInfo().getQuaRank();
        switch (quaRank){
            case JlQuaTemp.RUNK_JIA:
                //计算甲级减分
                return reduceJY(jlQuaTemp);
            case JlQuaTemp.RUNK_YI:
                //计算乙级减分
                return reduceJY(jlQuaTemp);
            case JlQuaTemp.RUNK_BING:
                //计算丙级减分
                return reduceBing(jlQuaTemp);
            case JlQuaTemp.RUNK_ZH:
                //计算综合减分
                return reduceZh(jlQuaTemp);
        }
        return 0D;
    }

    /**
     * 计算综合减分
     * @param jlQuaTemp
     * @return
     */
    private Double reduceZh(JlQuaTemp jlQuaTemp) {
        int personCount = jlQuaTemp.getJlPersonList().size();
        Double rScore = getReduceScore(personCount,ZH_60,ZH_70,ZH_80,ZH_90,ZH_100);
        if(rScore != null) return rScore;
        return 24D;
    }

    public Double getReduceScore(int personCount,Double S_60,Double S_70,Double S_80,Double S_90,Double S_100){
        if(personCount == S_100){
            return 0D;
        }
        if(personCount >= S_90 && personCount < S_100){
            return 2.4;
        }
        if(personCount >= S_80 && personCount < S_90){
            return 4.8;
        }
        if(personCount >= S_70 && personCount < S_80){
            return 7.2;
        }
        if(personCount >= S_60 && personCount < S_70){
            return 9.6;
        }
        return null;
    }

    /**
     * 计算丙减分
     * @param jlQuaTemp
     * @return
     */
    private Double reduceBing(JlQuaTemp jlQuaTemp) {
        int personCount = jlQuaTemp.getJlPersonList().size();
        Double rScore = getReduceScore(personCount,BING_60,BING_70,BING_80,BING_90,BING_100);
        if(rScore != null) return rScore;
        return 24D;
    }

    /**
     * 计算甲乙减分
     * @param jlQuaTemp
     * @return
     */
    public Double reduceJY(JlQuaTemp jlQuaTemp){
        //符合规则的人数 - 缺少的人数 = 人数
        int personCount = JlQuaTemp.JY_MIN_SUM_COUNT - jlQuaTemp.getShortOfCount();
        Double rScore = getReduceScore(personCount,JY_60,JY_70,JY_80,JY_90,JY_100);
        if(rScore != null) return rScore;
        return 24D;
    }

    /**
     * 获取所有企业是否符合资质标准
     * @return
     */
    public boolean getAllFlag(){
        for (JlQuaTemp jlQuaTemp : quaTempList) {
            if(!jlQuaTemp.getQualified()){
                return false;
            }
        }
        return true;
    }

    public Double getScore() {
        return score;
    }
}
